package com.admin.system.modules.mnt.service.impl;

import com.admin.common.exception.BadRequestException;
import com.admin.system.modules.mnt.dto.AppQueryDTO;
import com.admin.system.modules.mnt.entity.App;
import com.admin.system.modules.mnt.mapper.AppMapper;
import com.admin.system.modules.mnt.mapper.DeployMapper;
import com.admin.system.modules.mnt.mapper.DeployServerMapper;
import com.admin.system.modules.mnt.service.AppService;
import com.admin.common.utils.PageResult;
import com.admin.common.utils.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;

/**
 * @author sq-lan
 */
@Service
@RequiredArgsConstructor
public class AppServiceImpl extends ServiceImpl<AppMapper, App> implements AppService {

    private final DeployMapper deployMapper;
    private final DeployServerMapper deployServerMapper;

    @Override
    public PageResult<App> paging(AppQueryDTO dto) {
        PageHelper.startPage(dto.getCurrent(), dto.getSize());
        List<App> list = this.list(dto);
        return PageUtil.toPageHelper(list);
    }

    @Override
    public List<App> list(AppQueryDTO dto) {
        LambdaQueryWrapper<App> wrapper = Wrappers.lambdaQuery();
        if (StringUtils.hasText(dto.getName())) {
            wrapper.like(App::getName, dto.getName());
        }
        if (dto.getCreateTime() != null && !dto.getCreateTime().isEmpty()) {
            wrapper.between(App::getCreateTime, dto.getCreateTime().get(0), dto.getCreateTime().get(1));
        }
        return this.list(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(App entity) {
        verification(entity);
        save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modify(App entity) {
        verification(entity);
        updateById(entity);
    }

    private void verification(App entity) {
        String opt = "/opt";
        String home = "/home";
        if (!(entity.getUploadPath().startsWith(opt) || entity.getUploadPath().startsWith(home))) {
            throw new BadRequestException("文件只能上传在opt目录或者home目录 ");
        }
        if (!(entity.getDeployPath().startsWith(opt) || entity.getDeployPath().startsWith(home))) {
            throw new BadRequestException("文件只能部署在opt目录或者home目录 ");
        }
        if (!(entity.getBackupPath().startsWith(opt) || entity.getBackupPath().startsWith(home))) {
            throw new BadRequestException("文件只能备份在opt目录或者home目录 ");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void remove(Set<Integer> ids) {
        // 删除应用
        removeByIds(ids);
        // 删除部署
        Set<Integer> deployIds = deployMapper.getIdByAppIds(ids);
        if (deployIds != null && deployIds.size() > 0) {
            deployServerMapper.deleteByDeployIds(deployIds);
            deployMapper.deleteBatchIds(deployIds);
        }
    }

}
